// const STORAGE_KEY = "todos-vuejs";
import axios from 'axios'
import store from '../../../store/index'
axios.defaults.timeout = 5000
axios.defaults.crossDomain = true
axios.defaults.withCredentials = true
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded'

const baseUrl = process.env.NODE_ENV === 'production' ? '' : '/malltest'
const loginUrl =
  process.env.NODE_ENV === 'production' ? 'http://logintest.genebook.com.cn' : '/logintest'
// const baseUrl = '/malltest'
// const loginUrl = '/logintest'
const axiosGet = function(url, params = {}) {
  return new Promise((resolve, reject) => {
    axios
      .get(url, { params })
      .then(res => {
        resolve(res.data)
      })
      .catch(error => {
        reject(error)
      })
  })
}
const axiosPost = function(url, data = {}) {
  return new Promise((resolve, reject) => {
    axios
      .post(url, data)
      .then(res => {
        resolve(res.data)
      })
      .catch(error => {
        reject(error)
      })
  })
}
// 首页 banner
const getIndexBanner = params => {
  return axiosGet(`${baseUrl}/m/ajax/index/banner`, params)
}
// 首页商品列表
const getIndexLists = params => {
  return axiosGet(`${baseUrl}/m/ajax/index/list`, params)
}
// 首页，主分类查看更多
const getCategoryLists = params => {
  return axiosGet(`${baseUrl}/m/ajax/index/list/more`, params)
}

// 首页，主分类总数
const getCategoryTotal = params => {
  return axiosGet(`${baseUrl}/m/ajax/index/list/more/totalCount`, params)
}
// 首页，获取分类
const getCategory = params => {
  return axiosGet(`${baseUrl}/m/ajax/category`, params)
}

// 产品详情
const getProductDetail = params => {
  return axiosGet(`${baseUrl}/m/ajax/allBusiness/info`, params)
}
// 产品详情规格
const getProductGroup = params => {
  return axiosGet(`${baseUrl}/m/ajax/allBusiness/group`, params)
}

// 获取购物车
const getCart = params => {
  axiosGet(`${baseUrl}/m/ajax/cart`, params)
    .then(data => {
      console.log(data)
      if (data.flag) {
        // this.clearCart()
        // console.log('lll')
        store.dispatch('cart/clear_cart')
        if (data.data) {
          data.data.forEach(item => {
            // console.log(item)
            store.dispatch('cart/create_goods_to_cart', item)
            // this.createGoodsToCart(item)
            if (item.selectStatus == '1') {
              store.dispatch('cart/create_goods_to_selected', item)
              // this.createGoodsToSelected(item)
            }
          })
        }
      } else {
        alert(data.msg)
      }
    })
    .catch(err => {
      alert(err)
    })
}
//添加到购物车
const addToCart = params => {
  return axiosGet(`${baseUrl}/m/ajax/cart/add`, params)
}

// 购物车 更新
const updateCart = params => {
  return axiosGet(`${baseUrl}/m/ajax/cart/update`, params)
}

//购物车 删除
const deleteCart = params => {
  return axiosGet(`${baseUrl}/m/ajax/cart/delete`, params)
}

// 搜索
const searcher = params => {
  return axiosGet(`${baseUrl}/m/ajax/list`, params)
}

// 发送手机验证码
const sendRandomPwd = data => {
  return axiosPost(`${loginUrl}/random_password`, data)
}

// 登录
const loginSubmit = data => {
  return axiosPost(`${loginUrl}/login_submit`, data)
}
// 忘记密码获取验证码
const submitGetCheck = data => {
  return axiosPost(`${loginUrl}/password_forget_submit`, data)
}

// 登录验证码
const randomYzm = data => {
  return axiosPost(`${loginUrl}/login/jsp/code.jsp?id=${Math.random()}`, data)
}

// 注册账号获取验证码
const getRegisterCode = data => {
  return axiosPost(`${loginUrl}/register_submit`, data)
}

// 注册账号提交
const registerSubmit = data => {
  return axiosPost(`${loginUrl}/register_active_submit`, data)
}

// 修改密码验证码
const resetRandomYzm = data => {
  return axiosPost(`${loginUrl}/password/jsp/code.jsp?id=${Math.random()}`, data)
}

// 修改密码验提交
const resetPassword = data => {
  return axiosPost(`${loginUrl}/password_forget_active_submit`, data)
}

// 用户登录信息
const loginState = params => {
  return axiosGet(`${baseUrl}/m/ajax/uinfo`, params)
}
// 用户退出登录
const logout = params => {
  return axiosGet(`${loginUrl}/logout`, params)
}

export default {
  /**
   * 首页
   */
  getIndexBanner, // 首页 banner
  getIndexLists, // 首页商品列表
  getCategoryLists, // 首页，主分类查看更多
  getCategory, // 首页，获取分类
  getCategoryTotal, // 首页，主分类总数
  /**
   * 详情页
   */
  getProductDetail, // 产品详情
  getProductGroup, // 产品详情规格
  /**
   * 购物车
   */
  getCart, // 获取购物车
  updateCart, // 购物车 更新
  addToCart, //添加到购物车
  deleteCart, //购物车 删除
  /**
   * 搜索
   */
  searcher, // 搜索
  /**
   * 注册 登录
   */
  loginState, // 获取用户登录状态及信息
  logout, // 退出登录
  sendRandomPwd, // 发送手机验证码
  randomYzm, // 登录验证码
  resetRandomYzm, // 修改密码验证码
  loginSubmit, // 登录
  submitGetCheck, // 忘记密码获取验证码
  resetPassword, // 修改密码验提交
  getRegisterCode, //注册账号获取验证码
  registerSubmit //注册账号提交
}
